package com.yyjj.hangqing.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.vise.xsnow.http.ViseHttp;
import com.vise.xsnow.http.callback.ACallback;
import com.yyjj.hangqing.HqNetUrl;
import com.yyjj.hangqing.R;
import com.yyjj.hangqing.adapter.ZdfAdpter;
import com.yyjj.hangqing.cache.AppCacheData;
import com.yyjj.hangqing.entity.Zdentity1;

import tech.com.commoncore.base.BaseFragment;
import tech.com.commoncore.constant.ApiConstant;
import tech.com.commoncore.entity.BaseTResp;

/**
 * 涨跌幅榜
 */
public class ZfbFragment extends BaseFragment {
    RecyclerView recyclerView;
    ZdfAdpter zdfAdpter;

    @Override
    public int getContentLayout() {
        return R.layout.hq_fragment_zfb;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        zdfAdpter = new ZdfAdpter();
        recyclerView = mContentView.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(zdfAdpter);
        zdfAdpter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("name", zdfAdpter.getData().get(position).getCoin_name_base());
                bundle.putString("symbol", zdfAdpter.getData().get(position).getCoin_name().toUpperCase());
                ARouter.getInstance().build("/hq/kline/coin").with(bundle).navigation();
            }
        });
        getZDBData();
    }

    public static ZfbFragment newInstance(int type) {
        Bundle args = new Bundle();
        args.putInt("type", type);
        ZfbFragment fragment = new ZfbFragment();
        fragment.setArguments(args);
        return fragment;
    }

    void getZDBData() {
        ViseHttp.GET(HqNetUrl.ZDB)
                .baseUrl(ApiConstant.BASE_URL)
                .tag(HqNetUrl.ZDB).request(new ACallback<BaseTResp<Zdentity1>>() {

            @Override
            public void onSuccess(BaseTResp<Zdentity1> data) {
                if (getArguments().getInt("type") == 0) {
                    if (AppCacheData.getzf(mContext).size() == 0) {
                        AppCacheData.setzfbInfo(mContext, data.data.getRise_data());
                    }
                    zdfAdpter.replaceData(data.data.getRise_data());
                    Log.w("linbin---1",""+data.data.getRise_data().toString());
                } else {
                    if (AppCacheData.getzf1(mContext).size() == 0) {
                        AppCacheData.setzfbInfo1(mContext, data.data.getRise_data());
                    }
                    zdfAdpter.replaceData(data.data.getFall_data());
                    Log.w("linbin---2",""+data.data.getFall_data().toString());
                }

            }

            @Override
            public void onFail(int errCode, String errMsg) {
                Log.e("linbin---3",""+errMsg);
            }
        });
    }
}
